package com.javasm.mingming.socket;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * @author javasm
 * @version 0.1
 * @className TestSocket
 * @descriptioin:
 * @date 2025/1/20 15:05
 * @since jdk11
 */
@Component
@ServerEndpoint("/test/socket")
public class TestSocket {

    Logger logger = LogManager.getLogger(TestSocket.class);

    @OnOpen
    public void open(Session session){
        //import javax.websocket.Session;
        logger.info("------获取连接--------"+session);
        logger.info("客户端,连接到服务端的时候执行的方法@OnOpen");
    }
    @OnClose
    public void close(Session session){
        logger.info("客户端关闭连接的时候执行,断开连接:"+session);
    }

    @OnError
    public void error(Throwable throwable){
        logger.info("连接产生异常");
    }

    @OnMessage
    public void message(Session session,String msg) throws IOException {
        logger.info("接收客户端发来的消息:"+msg);
        //发消息给客户端.每一个客户端,都是一个session对象
        session.getBasicRemote().sendText("服务端接收到你的消息了");
    }


}
